﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings.Fakes;
using Sustainalytics.ClientDW.DataAccessLayer.Fakes;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.ESGRatings.Fakes;
using Sustainalytics.Entities.ESGRatings;


namespace Sustainalytics.ESGRatings.BusinessLogic.Tests
{
    [TestClass]
    public class WeightMatricesManagementTests
    {
        //[TestMethod]
        //public void EnsureWeightsConsistencyOnCompanies()
        //{
        //    const string weightMatrixName = "Default Weight Matrix";
        //    var weightMatrixId = Guid.NewGuid();
        //    var indicators = new List<string> { "A.1.2", "B.2.1", "C.11.22" };
        //    var esgCompanies = new List<CorporateData>
        //    {
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = 1, 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup1", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type A"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = 2, 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup2", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type B"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = 3, 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup2", 
        //            CompanyType = "Public", 
        //            TemplateType = "Type B"
        //        }, 
        //        new CorporateData
        //        {
        //            Id = Guid.NewGuid(), 
        //            CapitalIQID = 4, 
        //            HasEsgResearch = true, 
        //            RootPeerGroup = "PeerGroup1", 
        //            CompanyType = "Private", 
        //            TemplateType = "Type B"
        //        }
        //    };

        //    var newlyAddedCompanyWeights = new List<CompanyWeights>();

        //    var weightMatrix = new WeightMatrix
        //    {
        //        Id = weightMatrixId,
        //        AccountId = null,
        //        Name = weightMatrixName,
        //        IndicatorsNumbers = indicators                
        //    };

        //    var matrixPeerGroupWeights = new List<PeerGroupWeights>
        //    {
        //        new PeerGroupWeights
        //        {
        //            Id = Guid.NewGuid(),
        //            MatrixId = weightMatrixId,
        //            PeerGroupName = "PeerGroup1",
        //            CompanyType = "Public",
        //            TemplateType = "Type A",
        //            Weights =
        //                indicators.Select((i, idx) => new KeyValuePair<string, float>(i, idx))
        //                    .ToDictionary(i => i.Key, i => i.Value)
        //        },
        //        new PeerGroupWeights
        //        {
        //            Id = Guid.NewGuid(),
        //            MatrixId = weightMatrixId,
        //            PeerGroupName = "PeerGroup2",
        //            CompanyType = "Public",
        //            TemplateType = "Type B",
        //            Weights =
        //                indicators.Select((i, idx) => new KeyValuePair<string, float>(i, idx))
        //                    .ToDictionary(i => i.Key, i => i.Value)
        //        }
        //    };

        //    var matrixCompanyWeights = new List<CompanyWeights>
        //    {
        //        new CompanyWeights
        //        {
        //            Id = Guid.NewGuid(),
        //            MatrixId = weightMatrixId,
        //            CompanyId = esgCompanies[0].Id,
        //            Weights =
        //                indicators.Select((i, idx) => new KeyValuePair<string, float>(i, idx))
        //                    .ToDictionary(i => i.Key, i => i.Value)
        //        },
        //        new CompanyWeights
        //        {
        //            Id = Guid.NewGuid(),
        //            MatrixId = weightMatrixId,
        //            CompanyId = esgCompanies[2].Id,
        //            Weights =
        //                indicators.Select((i, idx) => new KeyValuePair<string, float>(i, idx))
        //                    .ToDictionary(i => i.Key, i => i.Value)
        //        }
        //    };

        //    var clientDw = new StubIClientDwRepository
        //    {
        //        ListCompaniesIdsNullableOfBoolean = hasEsgResearch => esgCompanies.Where(c => hasEsgResearch == null || c.HasEsgResearch == hasEsgResearch)
        //                                                                            .ToDictionary(c => c.CapitalIQID, c => c.Id), 
                
        //        ListCompaniesCorporateDataIEnumerableOfGuid = companiesIds => esgCompanies.Where(c => companiesIds.Contains(c.Id)).ToList()
                
        //    };
        //    var esgInputFileRepository = new StubIEsgInputFileRepository();
        //    var weightMatricesRepository = new StubIWeightMatricesRepository
        //    {
        //        ListWeightMatrices = () => new List<WeightMatrix> { weightMatrix }, 
        //        ListPeerGroupWeightsIEnumerableOfGuid = matricesIds => matrixPeerGroupWeights.Where(pgw => matricesIds.Contains(pgw.MatrixId))
        //                                                                                .GroupBy(pgw => pgw.MatrixId)
        //                                                                                .ToDictionary(mpgw => mpgw.Key, mpgw => mpgw.ToList()),
        //        ListCompanyWeightsIEnumerableOfGuid = matricesIds => matrixCompanyWeights.Where(cw => matricesIds.Contains(cw.MatrixId))
        //                                                                                .GroupBy(cw => cw.MatrixId)
        //                                                                                .ToDictionary(mcw => mcw.Key, mpgw => mpgw.ToList()), 
        //        AddCompanyWeightsIEnumerableOfCompanyWeights = companyWeights => newlyAddedCompanyWeights.AddRange(companyWeights)
        //    };
         

        //    //var weightMatricesManagement = WeightMatricesManagement.GetInstance(clientDw, esgInputFileRepository, weightMatricesRepository, redisCache);

        //    //weightMatricesManagement.EnsureWeightsConsistencyOnCompanies();

        //    //Assert.IsTrue(newlyAddedCompanyWeights.Any(), "No company weights newly added!");
        //    //Assert.IsTrue(newlyAddedCompanyWeights.Any(cw => cw.MatrixId == weightMatrix.Id && cw.CompanyId == esgCompanies[1].Id), "The company lacking weights was not added as expected!");
        //}
    }
}
